/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.data.resource;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Responsible for i18n messages. 
 * TODO: Use "edu.cmu.lti.sepia.data.Lang" class here
 * 
 * @author hideki
 *
 */
public class MessageResource {

	private static Map<String, String> enMap;
	private static Map<String, String> csMap;
	private static Map<String, String> ctMap;
	private static Map<String, String> jaMap;
	
	static {
		init();
	}
	
	private static void init() {
		// FIXME: cs -> en for avoid null
		//if ( enMap!=null ) return;
		enMap = loadProperties( "en" );
		csMap = loadProperties( "cs" );
		ctMap = loadProperties( "ct" );
		jaMap = loadProperties( "ja" );	
	}
	
	private static LinkedHashMap<String, String> loadProperties( String lang ) {
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		
		try {		  
			ClassLoader cl = Thread.currentThread().getContextClassLoader();
			InputStream is = cl.getResourceAsStream( "resources/MessageResources_"+lang+".properties" );
			
			//Supported in JDK 6 or later only
			//InputStreamReader reader1 = new InputStreamReader( is, "utf-8" );
						
			Properties p = new Properties();
			p.load(is);
			
			for ( Object o : p.keySet() ) {
			  try {
  				String key = new String(o.toString().getBytes("ISO8859_1"), "UTF8");
  				String value = new String(p.getProperty(o.toString()).getBytes("ISO8859_1"), "UTF8");
  				map.put(key, value);
			  } catch (Exception e) {
			    System.err.println("Message resource problem: "+lang+" . "+o.toString());
			  }
			}
			
			// Do it again for QuestionTypes	        	
		} catch (Exception e) {
			e.printStackTrace();
		} 

		return map;
	}
		
	public static String getString( String lang, String key ) {
		String msg = "-";
		
		init();
	
		try {
			if ( lang == null || lang.matches("EN") ) {
				msg = enMap.get( key );
			} else if ( lang.matches("CS") ) { 
				msg = csMap.get( key );
			} else if ( lang.matches("CT") ) { 
				msg = ctMap.get( key );
			} else if ( lang.matches("JA") ) { 
				msg = jaMap.get( key );
			}
			
			if ( msg == null ) msg = "null?";
			
			msg = msg.trim();
		} catch ( Exception e ) {
			msg = "error";
		}
		
		return msg;
	}
	
	//Don't forget to var "+var+" = new Array(); in JS
	public static String messageResourceToJs( String lang, String var ) {
		init();
		
		StringBuilder sb = new StringBuilder();
		
		Map<String, String> map = null; 
		if ( lang == null || lang.matches("EN") ) {
			map  = enMap;
		} else if ( lang.matches("CS") ) { 
			map  = csMap;
		} else if ( lang.matches("CT") ) { 
			map  = ctMap;
		} else if ( lang.matches("JA") ) { 
			map  = jaMap;
		}
		
		for ( String key : map.keySet() ) {
			sb.append( var+"['"+lang+"."+key+"'] = '"+map.get(key).replaceAll("'", "\\\\'")+"';\n" );
		}
		
		return sb.toString();
	}

}

